package com.jbh.permissionlibrary.base;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import com.jbh.permissionlibrary.PermissionManager;
import com.jbh.permissionlibrary.i.PermissionRequestCode;
import java.util.ArrayList;
import java.util.List;

public class PermissionsResult  {

    private AlertDialog.Builder alertDialog ;
    private Activity context;

    public PermissionsResult(Activity context,int requestCode, String[] permissions,int[] grantResults){
        this.context = context ;
        alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle("提示");
        requestPermissionsResult( requestCode,  permissions, grantResults);
    }

    private void requestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        for (int i = 0; i < permissions.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                //【1】始终允许
                onAgreePermission(requestCode);
            } else {
                if (!ActivityCompat.shouldShowRequestPermissionRationale(context, permissions[i])) {
                    //【3】禁止不再询问
                    initNoAskedPermission(requestCode);
                } else {
                    //【2】禁止
                    initBanPermission(requestCode);
                }
            }
        }
    }



    /**
     * 同意了权限
     *
     * 留给子类去实现
     *
     * {@link PermissionRequestCode}
     *
     * @param requestCode
     */
    protected  void onAgreePermission(int requestCode){


    }



    /**
     * 禁止不在询问
     *
     * @param requestCode
     */
    private  void initNoAskedPermission(int requestCode){
        String text = "此功能所需权限已经被您禁止，请开启才能使用此功能！";
        switch (requestCode){
            case PermissionRequestCode.REQUEST_CODE:
                alertDialog.setMessage("您已经禁止此功能需要的权限，需要开启权限才能使用此功能！！！");
                break;
            case PermissionRequestCode.WRITE_EXTERNAL_STORAGE_REQUEST_CODE:
                text = "无法使用储存，请在“设置>应用管理>找金>媒体和文件”中开启";
                break;
            case PermissionRequestCode.CALL_PHONE_REQUEST_CODE:
            case PermissionRequestCode.RECORD_AUDIO_REQUEST_CODE:
                text = "无法使用麦克风，请在“设置>应用管理>找金>麦克风”中开启";
                break;
            case PermissionRequestCode.CAMERA_REQUEST_CODE:
                text = "无法使用摄像头，请在“设置>应用管理>找金>相机”中开启";
                break;
            case PermissionRequestCode.WRITE_EXTERNAL_STORAGE_AND_CAMERA_REQUEST_CODE:
                text = "无权访问存储和相机，请点击【去开启】->设置->选择权限->存储或相机->勾选允许！";
                break;
            case PermissionRequestCode.ACCESS_COARSE_LOCATION:
                text = "无法使用位置服务，请在“设置>应用管理>找金>位置信息”中开启";
                break;
            case PermissionRequestCode.WRITE_EXTERNAL_STORAGE_AND_INSERT_REQUEST_CODE:
                text="无权访问读写或安装应用权限，请点击【去开启】选择存储或应用内安装->“允许”才可使用此功能！";
                break;
        }

        alertDialog.setMessage(text);
        alertDialog.setPositiveButton("取消",null);
        alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if(Build.VERSION.SDK_INT >= 9){
                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                } else if(Build.VERSION.SDK_INT <= 8){
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
                    intent.putExtra("com.android.settings.ApplicationPkgName",context. getPackageName());
                }
                context.  startActivity(intent);
            }
        });
        alertDialog.show();
    }


    /**
     * 禁止权限
     *
     * @param requestCode
     */
    private void initBanPermission(int requestCode){
        alertDialog.setMessage("此功能所需权限已经被您禁止，请重试，选择允许才能使用此功能！");
        switch (requestCode){
            case PermissionRequestCode.REQUEST_CODE:
                alertDialog.setMessage("您已经禁止此功能需要的权限，需要开启权限才能使用此功能！！！");
                alertDialog.setNegativeButton("确定",null);
                break;
            case PermissionRequestCode.WRITE_EXTERNAL_STORAGE_REQUEST_CODE:
                alertDialog.setMessage("无权访问存储，请点击【去开启】选择“允许”才可使用此功能！！！");
                alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        PermissionManager.newInstance().checkPermission(
                                context,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                PermissionRequestCode.WRITE_EXTERNAL_STORAGE_REQUEST_CODE,
                                null);
                    }
                });
                break;
            case PermissionRequestCode.CALL_PHONE_REQUEST_CODE:
                alertDialog.setMessage("无权访问拨打电话，请点击【去开启】选择“允许”才可使用此功能！！！");
                alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        PermissionManager.newInstance().checkPermission(
                                context,
                                Manifest.permission.CALL_PHONE,
                                PermissionRequestCode.CAMERA_REQUEST_CODE,null);
                    }
                });
                break;
            case PermissionRequestCode.CAMERA_REQUEST_CODE:
                alertDialog.setMessage("无权访问相机，请点击【去开启】选择“允许”才可使用此功能！！！");
                alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        PermissionManager.newInstance().checkPermission(
                                context,
                                Manifest.permission.CAMERA,
                                PermissionRequestCode.CAMERA_REQUEST_CODE,null);
                    }
                });
                break;
            case PermissionRequestCode.RECORD_AUDIO_REQUEST_CODE:
                alertDialog.setMessage("无权访问录音，请点击【去开启】选择“允许”才可使用此功能！！！");
                alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        PermissionManager.newInstance().checkPermission(
                                context,
                                Manifest.permission.RECORD_AUDIO,
                                PermissionRequestCode.RECORD_AUDIO_REQUEST_CODE,null);
                    }
                });
                break;
            case PermissionRequestCode.WRITE_EXTERNAL_STORAGE_AND_CAMERA_REQUEST_CODE:
                alertDialog.setMessage("无权访问存储和相机，请点击【去开启】选择“允许”才可使用此功能！");
                alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        List<String> list= new ArrayList<>();
                        list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                        list.add(Manifest.permission.CAMERA);
                        PermissionManager.newInstance().checkPermission(
                                context,
                                list,
                                PermissionRequestCode.WRITE_EXTERNAL_STORAGE_AND_CAMERA_REQUEST_CODE,
                                null);
                    }
                });
                break;
            case PermissionRequestCode.ACCESS_COARSE_LOCATION:
                alertDialog.setMessage("无权访问位置信息，请点击【去开启】选择“允许”才可使用此功能！");
                alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        List<String> list= new ArrayList<>();
                        list.add(Manifest.permission.ACCESS_FINE_LOCATION);
                        list.add(Manifest.permission.ACCESS_COARSE_LOCATION);
                        PermissionManager.newInstance().checkPermission(
                                context,
                                list,
                                PermissionRequestCode.ACCESS_COARSE_LOCATION,
                                null);
                    }
                });
                break;

            case PermissionRequestCode.WRITE_EXTERNAL_STORAGE_AND_INSERT_REQUEST_CODE:
                alertDialog.setMessage("无权访问读写或安装应用权限，请点击【去开启】选择存储或应用内安装->“允许”才可使用此功能！");
                alertDialog.setNegativeButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        List<String> list= new ArrayList<>();
                        list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                        list.add(Manifest.permission.REQUEST_INSTALL_PACKAGES);
                        PermissionManager.newInstance().checkPermission(
                                context,
                                list,
                                PermissionRequestCode.WRITE_EXTERNAL_STORAGE_AND_INSERT_REQUEST_CODE,
                                null);
                    }
                });
                break;
        }
        alertDialog.setPositiveButton("取消",null);
        alertDialog.show();
    }
}
